home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / OPENIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  2.8 KB  |  101 lines

  1. /* openix.c - open / close an index file for BTREE */
  2. #include   "stdio.h"
  3. #include   "cminor.h"
  4. #include   "btree.h"
  5. #include   "bt_macro.h"
  6.  
  7. extern     IX_DESC    *pci ;    /* refers to index descriptor */
  8.                 /* for current function call  */
  9.  
  10. RECPOS    wrt_dum() ;
  11.  
  12. int  openix(name,pix,cfun,sfun) /* open an existing index file */
  13.   char    name[] ;        /* file name */
  14.   IX_DESC  *pix ;        /* control block for index */
  15.   int    (*cfun) () ;        /* address of compare function */
  16.   int    (*sfun) () ;        /* address of entry size function */
  17.   {
  18.      int   ret ;
  19.  
  20.      pci = pix ;
  21.      ret = open_if(name) ;
  22.      if( ret < 0 )        /* check for failure to create file */
  23.     return( IX_FAIL ) ;
  24.      pci->ixfile = ret ;
  25.      read_if(0L,&pix->dx, sizeof(IX_DISK) ) ;
  26.      pci->pcomp = cfun ;    /* record address of compare fun. */
  27.      pci->psize = sfun ;    /* and of entry size function */
  28.      init_bio() ;        /* initialize block I/O */
  29.      go_first(pix) ;        /* position at start of file */
  30.      return( IX_OK ) ;
  31.   }
  32.  
  33. int close(pix)            /* close an index file */
  34.   IX_DESC  *pix ;        /* index descriptor */
  35.   {
  36.      pci = pix ;
  37.      write_if(0L,&pci->dx, sizeof( IX_DISK ) ) ;
  38.      close_if();        /* close the index file */
  39.   }
  40.  
  41.  
  42. int  creatix(name,filesize,pdum,ndum) /* create a new index file */
  43.   char    name[] ;        /* name of file */
  44.   long    filesize ;        /* size of the file */
  45.   ENTRY *pdum ;         /* dummy entry for EOF */
  46.   int    ndum ;            /* size of dummy entry */
  47.   {
  48.      BLOCK b ;
  49.      IX_DESC  ixd ;
  50.      int   ret ;
  51.  
  52.      pci = & ixd ;
  53.      ret = creat_if(name) ;
  54.      if( ret < 0 )        /* check for failure to create file */
  55.     return( IX_FAIL ) ;
  56.      ixd.ixfile = ret ;
  57.      ixd.dx.nl = MAX_LEVELS ;    /* all levels present */
  58.      mover(&ixd.dx.dume,pdum,ndum) ;
  59.      clr_mem(&b,0,IXB_SIZE) ;    /* make a block of zeros */
  60.      write_block(0L,&b) ;    /* write it at BOF */
  61.                 /* set up index block for each level */
  62.                 /* and record location of root block */
  63.      ixd.dx.rb = wrt_dum(pdum,ndum) ;
  64.                 /* set up free block list */
  65.                 /* start it after dummy blocks */
  66.      make_free( ((RECPOS) (MAX_LEVELS+1)) * IXB_SIZE, filesize) ;
  67.      closeix(&ixd) ;        /* close file updating desc. info */
  68.      return( IX_OK ) ;
  69.   }
  70.  
  71. RECPOS    wrt_dum(pdum,ndum)    /* write index block for each level */
  72.   ENTRY *pdum ;         /* dummy entry */
  73.   int    ndum ;            /* size of dummy entry */
  74.   {
  75.      BLOCK b ;
  76.      int   l ;
  77.      RECPOS   r ;
  78.  
  79.      pdum->rptr = NULLREC ;
  80.      r = 0 ;
  81.      for( l=0 ; l < MAX_LEVELS ; l=l+1 )
  82.     {  r = r + IXB_SIZE ;
  83.        mover( b.entries,pdum,ndum) ;/* put dummy in block */
  84.        b.lvl = l ;
  85.        b.bend = ndum ;    /* block contains one entry */
  86.        write_block(r,&b) ;    /* write the block */
  87.        pdum->rptr = r ;
  88.     }
  89.      return( r ) ;
  90.   }
  91.  
  92.  
  93. int removeix(name)        /* delete an index file */
  94.   char    name[] ;        /* name of the file */
  95.   {
  96.      unlink(name) ;
  97.   }
  98.  
  99.  
  100.  
  101.